package com.example.jsq;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity  {
    private TextView calculator_monitor;
    private Button   but_0,but_1,but_2,but_3,but_4,but_5,but_6,but_7,but_8,but_9,but_clear,but_divide,
            but_multiply,but_add,but_minus,but_radic,but_dot,but_equal,but_percent;
    double num1=0,num2=0;
    double result=0;//Calculation results
    int operation=0;//Operands

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calculator_monitor=(TextView)findViewById(R.id.calculator_screen);
        but_0=(Button)findViewById(R.id.button_0);
        but_1=(Button)findViewById(R.id.button_1);
        but_2=(Button)findViewById(R.id.button_2);
        but_3=(Button)findViewById(R.id.button_3);
        but_4=(Button)findViewById(R.id.button_4);
        but_5=(Button)findViewById(R.id.button_5);
        but_6=(Button)findViewById(R.id.button_6);
        but_7=(Button)findViewById(R.id.button_7);
        but_8=(Button)findViewById(R.id.button_8);
        but_9=(Button)findViewById(R.id.button_9);
        but_add=(Button)findViewById(R.id.button_add);
        but_clear=(Button)findViewById(R.id.button_clear);
        but_divide=(Button)findViewById(R.id.button_divide);
        but_dot=(Button)findViewById(R.id.button_dot);
        but_equal=(Button)findViewById(R.id.button_equal);
        but_minus=(Button)findViewById(R.id.button_minus);
        but_multiply=(Button)findViewById(R.id.button_multiply);
        but_radic=(Button)findViewById(R.id.button_radic);
        but_percent=(Button)findViewById(R.id.button_percent);


        but_0.setOnClickListener(new MyOnClickListener());//加监视器
        but_1.setOnClickListener(new MyOnClickListener());
        but_2.setOnClickListener(new MyOnClickListener());
        but_3.setOnClickListener(new MyOnClickListener());
        but_4.setOnClickListener(new MyOnClickListener());
        but_5.setOnClickListener(new MyOnClickListener());
        but_6.setOnClickListener(new MyOnClickListener());
        but_7.setOnClickListener(new MyOnClickListener());
        but_8.setOnClickListener(new MyOnClickListener());
        but_9.setOnClickListener(new MyOnClickListener());
        but_clear.setOnClickListener(new MyOnClickListener());
        but_divide.setOnClickListener(new MyOnClickListener());
        but_minus.setOnClickListener(new MyOnClickListener());
        but_add.setOnClickListener(new MyOnClickListener());
        but_equal.setOnClickListener(new MyOnClickListener());
        but_multiply.setOnClickListener(new MyOnClickListener());
        but_dot.setOnClickListener(new MyOnClickListener());
        but_radic.setOnClickListener(new MyOnClickListener());
        but_percent.setOnClickListener(new MyOnClickListener());

    }
    class MyOnClickListener implements  View.OnClickListener{
        public void onClick(View v){//点击事件的处理方法
            switch (v.getId()){
                case R.id.button_clear:
                    calculator_monitor.setText(null);
                    break;
                case R.id.button_0:
                    String str0=calculator_monitor.getText().toString();//获取输入的内容并转换为string型
                    str0+="0";
                    calculator_monitor.setText(str0);
                    break;
                case R.id.button_1:
                    String str1=calculator_monitor.getText().toString();
                    str1+="1";
                    calculator_monitor.setText(str1);
                    break;
                case R.id.button_2:
                    String str2=calculator_monitor.getText().toString();
                    str2+="2";
                    calculator_monitor.setText(str2);
                    break;
                case R.id.button_3:
                    String str3=calculator_monitor.getText().toString();
                    str3+="3";
                    calculator_monitor.setText(str3);
                    break;
                case R.id.button_4:
                    String str4=calculator_monitor.getText().toString();
                    str4+="4";
                    calculator_monitor.setText(str4);
                    break;
                case R.id.button_5:
                    String str5=calculator_monitor.getText().toString();
                    str5+="5";
                    calculator_monitor.setText(str5);
                    break;
                case R.id.button_6:
                    String str6=calculator_monitor.getText().toString();
                    str6+="6";
                    calculator_monitor.setText(str6);
                    break;
                case R.id.button_7:
                    String str7=calculator_monitor.getText().toString();
                    str7+="7";
                    calculator_monitor.setText(str7);
                    break;
                case R.id.button_8:
                    String str8=calculator_monitor.getText().toString();
                    str8+="8";
                    calculator_monitor.setText(str8);
                    break;
                case R.id.button_9:
                    String str9=calculator_monitor.getText().toString();
                    str9+="9";
                    calculator_monitor.setText(str9);
                    break;
                case R.id.button_dot:
                    String strdot=calculator_monitor.getText().toString();
                    strdot+=".";
                    calculator_monitor.setText(strdot);
                    break;
                case R.id.button_add:
                    String stradd=calculator_monitor.getText().toString();
                    if(stradd.equals(null)){
                        return;
                    }
                    num1=Double.valueOf(stradd);
                    stradd+="+";
                    calculator_monitor.setText(null);
                    operation=1;
                    break;
                case R.id.button_minus:
                    String strminus=calculator_monitor.getText().toString();
                    if(strminus.equals(null)){
                        return;
                    }
                    num1=Double.valueOf(strminus);
                    strminus+="-";
                    calculator_monitor.setText(null);
                    operation=2;
                    break;
                case R.id.button_multiply:
                    String strmultiply=calculator_monitor.getText().toString();
                    if(strmultiply.equals(null)){
                        return;
                    }
                    num1=Double.valueOf(strmultiply);
                    strmultiply+="*";
                    calculator_monitor.setText(null);
                    operation=3;
                    break;
                case R.id.button_divide:
                    String strdivide=calculator_monitor.getText().toString();
                    if(strdivide.equals(null)){
                        return;
                    }
                    num1=Double.valueOf(strdivide);
                    strdivide+="/";
                    calculator_monitor.setText(null);
                    operation=4;
                    break;
                case R.id.button_percent:
                    String strpercent=calculator_monitor.getText().toString();
                    if(strpercent.equals(null)){
                        return;
                    }
                    num1=Double.valueOf(strpercent);
                    strpercent+="%";
                    operation=5;
                    break;



                case R.id.button_equal:
                    String strequ=calculator_monitor.getText().toString();
                    num2=Double.valueOf(strequ);
                    // calculator_monitor.setText(null);
                    switch (operation){
                        case 1:
                            result=num1+num2;
                            calculator_monitor.setText(String.valueOf(num1)+"+"+String.valueOf(num2)+"="+String.valueOf(result));
                            break;
                        case 2:
                            result=num1-num2;
                            calculator_monitor.setText(String.valueOf(num1)+"-"+String.valueOf(num2)+"="+String.valueOf(result));
                            break;
                        case 3:
                            result=num1*num2;
                            calculator_monitor.setText(String.valueOf(num1)+"×"+String.valueOf(num2)+"="+String.valueOf(result));
                            break;
                        case 4:
                            if(num2==0){
                                calculator_monitor.setText("被除数不能为0！");


                            }
                            else {
                                result = num1 / num2;
                                calculator_monitor.setText(String.valueOf(num1) + "÷" + String.valueOf(num2) + "=" + String.valueOf(result));
                            }
                            break;
                        case 5:
                            result=num1/100;
                            calculator_monitor.setText(String.valueOf(num1) + "%" +"="+String.valueOf(result));
                            break;

                        default:
                            result=0;
                            break;
                    }
            }

        }
    }
}
